<?php

class Plugins_InitialisationPlugin extends Plugins_StrategyPlugin {
	private $availablePlugins = array();
	private $actifPlugins = array();

	public function routeStartup(
	Zend_Controller_Request_Abstract $request) {
		$this->initAvailablePlugins();
		foreach ($this->getAvailablePlugins() as $plugin) {
			$plugin->routeStartup($request);
		}
	}

	public function routeShutdown(Zend_Controller_Request_Abstract $request) {
		$this->initActifPlugins($request);
		foreach ($this->getActifPlugins() as $plugin) {
			$plugin->routeShutdown($request);
		}
	}

	public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
		foreach ($this->getActifPlugins() as $plugin) {
			$plugin->dispatchLoopStartup($request);
		}
	}

	public function preDispatch(
	Zend_Controller_Request_Abstract $request) {
		foreach ($this->getActifPlugins() as $plugin) {
			$plugin->preDispatch($request);
		}
	}

	public function postDispatch(
	Zend_Controller_Request_Abstract $request) {
		foreach ($this->getActifPlugins() as $plugin) {
			$plugin->postDispatch($request);
		}
	}

	public function dispatchLoopShutdown() {
		foreach ($this->getActifPlugins() as $plugin) {
			$plugin->dispatchLoopShutdown();
		}
	}
	
	private function &getAvailablePlugins() {
		return $this->availablePlugins;
	}
	
	private function setAvailablePlugins(array $availablePlugins) {
		$this->availablePlugins = $availablePlugins;
	}

	public function getAvailablePlugin($namePlugin) {
		$availablePlugins = $this->getAvailablePlugins();
		return $availablePlugins[$namePlugin];
	}
	
	private function &getActifPlugins() {
		return $this->actifPlugins;
	}
	
	private function setActifPlugins(array $actifPlugins) {
		$this->actifPlugins = $actifPlugins;
	}

	private function addActifPlugin(Zend_Controller_Plugin_Abstract $plugin) {
		array_push($this->getActifPlugins(), $plugin);
	}

	private function initAvailablePlugins() {
		$robotsPlugin = new Plugins_RobotsPlugin();
		$robotsPlugin->setPlugin($this);
        $sitemapPlugin = new Plugins_SitemapPlugin();
        $sitemapPlugin->setPlugin($this);
		$internationalisationPlugin = new Plugins_InternationalisationPlugin();
		$internationalisationPlugin->setPlugin($this);
		$captchaPlugin = new Plugins_CaptchaPlugin();
		$captchaPlugin->setPlugin($this);
		$photosPlugin = new Plugins_PhotosPlugin();
		$photosPlugin->setPlugin($this);

		$this->setAvailablePlugins(array('internationalisation' => $internationalisationPlugin, 'captcha' => $captchaPlugin, 'robots' => $robotsPlugin, 'sitemap' => $sitemapPlugin, 'photos' => $photosPlugin));
	}


	private function initActifPlugins(
	Zend_Controller_Request_Abstract $request) {
		switch($request->getControllerName()) {
			case Plugins_RobotsPlugin::CONTROLLER_ROBOTS_DEFAULT:
				$this->addActifPlugin($this->getAvailablePlugin('robots'));
				break;
            case Plugins_SitemapPlugin::CONTROLLER_SITEMAP_DEFAULT:
                $this->addActifPlugin($this->getAvailablePlugin('sitemap'));
                break;
			case Plugins_CaptchaPlugin::CONTROLLER_CAPTCHA_DEFAULT:
				$this->addActifPlugin($this->getAvailablePlugin('captcha'));
				break;
			case Plugins_PhotosPlugin::CONTROLLER_PHOTOS_DEFAULT:
				$this->addActifPlugin($this->getAvailablePlugin('photos'));
				//no break
			default:
				$this->addActifPlugin($this->getAvailablePlugin('internationalisation'));
			break;
		}
	}
}